package it.application.smoc.List;

import it.application.smoc.CustomWindow;
import it.application.smoc.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CustomizedListView extends CustomWindow {
	// All static variables
	static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML node keys
	static final String KEY_CONTACT = "contatto"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_NOME = "nome";
	static final String KEY_COGNOME = "cognome";
	static final String KEY_DELEGAZIONE = "delegazione";
	static final String KEY_TITOLO = "titolo";
	static final String KEY_LUOGO_NASCITA = "luogo_nascita";
	static final String KEY_DATA_NASCITA = "data_nascita";
	static final String KEY_GRADO_ORDINE = "grado_ordine";
	static final String KEY_DATA_DECR_DIPL = "data_decr_dipl";
	static final String KEY_N_DIPLOMA = "n_diploma";
	static final String KEY_DATA_PROM = "data_prom";
	static final String KEY_N_DIPLOMA2 = "n_diploma2";
	static final String KEY_INDIRIZZO = "indirizzo";
	static final String KEY_CAP = "cap";
	static final String KEY_CITTA = "citta";
	static final String KEY_PROV = "prov";
	static final String KEY_TEL = "tel";
	static final String KEY_CELL = "cell";
	static final String KEY_EMAIL = "email";
	static final String KEY_PRIVACY = "privacy";
	static final String KEY_THUMB_URL = "thumb_url";
	
	ListView list;
    LazyAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		

		ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		//String xml = parser.getXmlFromUrl(URL); // getting XML from URL
		InputStream stream= null;
		try {
			stream = getAssets().open("contatti.xml");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String xml = convertStreamToString(stream);
		
		Document doc = parser.getDomElement(xml); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_CONTACT);
		// looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_NOME, parser.getValue(e, KEY_NOME));
			map.put(KEY_COGNOME, parser.getValue(e, KEY_COGNOME));
			map.put(KEY_DELEGAZIONE, parser.getValue(e, KEY_DELEGAZIONE));
			map.put(KEY_TITOLO, parser.getValue(e, KEY_TITOLO));
			map.put(KEY_LUOGO_NASCITA, parser.getValue(e, KEY_LUOGO_NASCITA));
			map.put(KEY_DATA_NASCITA, parser.getValue(e, KEY_DATA_NASCITA));
			map.put(KEY_GRADO_ORDINE, parser.getValue(e, KEY_GRADO_ORDINE));
			map.put(KEY_DATA_DECR_DIPL, parser.getValue(e, KEY_DATA_DECR_DIPL));
			map.put(KEY_N_DIPLOMA, parser.getValue(e, KEY_N_DIPLOMA));
			map.put(KEY_DATA_PROM, parser.getValue(e, KEY_DATA_PROM));
			map.put(KEY_N_DIPLOMA2, parser.getValue(e, KEY_N_DIPLOMA2));
			map.put(KEY_INDIRIZZO, parser.getValue(e, KEY_INDIRIZZO));
			map.put(KEY_CAP, parser.getValue(e, KEY_CAP));
			map.put(KEY_CITTA, parser.getValue(e, KEY_CITTA));
			map.put(KEY_PROV, parser.getValue(e, KEY_PROV));
			map.put(KEY_TEL, parser.getValue(e, KEY_TEL));
			map.put(KEY_CELL, parser.getValue(e, KEY_CELL));
			map.put(KEY_EMAIL, parser.getValue(e, KEY_EMAIL));
			map.put(KEY_PRIVACY, parser.getValue(e, KEY_PRIVACY));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

			// adding HashList to ArrayList
			contactList.add(map);
		}
		

		list=(ListView)findViewById(R.id.list);
		
		// Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, contactList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent( getApplicationContext(),
						DetailView.class
					);

				intent.putExtra("it.application.smoclist.Lista", adapter.getItems().get(position) );
				
				startActivity(intent);
				//Toast.makeText(view.getContext(), ((TextView)view.findViewById(R.id.id)).getText().toString() , Toast.LENGTH_SHORT ).show();

			}
		});		
	}
	String convertStreamToString(java.io.InputStream is) {
	    try {
	        return new java.util.Scanner(is).useDelimiter("\\A").next();
	    } catch (java.util.NoSuchElementException e) {
	        return "";
	    }
	}
}